<template>
  <div>
    <div class="card" style="margin-bottom: 10px">
      <el-input v-model="data.name" style="width:20%;border-radius: 5px ;
      margin-right: 10px" class="w-50 m-2" placeholder="输入学生姓名以查询" :prefix-icon="Search"/>
      <el-input v-model="data.username" style="width:20%;border-radius: 5px ;
      margin-right: 10px" class="w-50 m-2" placeholder="输入学生账号以查询"
                :prefix-icon="Search"/>
      <el-button type="primary" round style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="info" round @click="reset" style="margin-left: 10px">清除</el-button>
      <el-button type="primary" round @click="handleAdd" style="">添加</el-button>
    </div>
    <div class="card" style="margin-bottom: 10px">
      <el-table :data="data.tableData" style="width: 100%">
        <el-table-column prop="id" label="序号"/>
        <el-table-column prop="avatar" label="头像"  >
          <template #default="scope">
            <el-image class="listAvatar" v-if="scope.row.avatar" :src="scope.row.avatar"  />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="学生名称" />
        <el-table-column prop="username" label="学生账号" />
        <el-table-column prop="age" label="年龄" />
        <el-table-column prop="sex" label="性别"  />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="identi" label="身份证号码" />
        <el-table-column >
          <template #default="scope" >
            <el-button type="primary" style="margin-right: 10px;margin-bottom: 3px" @click="editHandler(scope.row)">编辑</el-button>
            <el-button type="danger" style="margin-left: 0px" @click="delHandler(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="card">
      <el-pagination v-model:current-page="data.pageNum" v-model:page-size="data.pageSize"
                     @current-change = "handleCurrentChange"
                     layout="prev, pager, next" :total="data.total"
      />
    </div>
    <el-dialog v-model="data.formVisible" title="学生信息" width="40%" destroy-on-close >
      <el-form :model="data.form" label-width="100px" ref="formRef" label-position="right" :rules="rules">
        <el-form-item prop="name" label="学生名称">
          <el-input v-model="data.form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="username" label="学生账号">
          <el-input v-model="data.form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="password" label="学生密码">
          <el-input v-model="data.form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="data.form.age" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别">
          <div>
            <el-radio-group v-model="radio2" >
              <el-radio label="男" border>男</el-radio>
              <el-radio label="女" border>女</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="data.form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="身份证号码">
          <el-input v-model="data.form.identi" autocomplete="off" />
        </el-form-item>
        <el-form-item label="头像" >
          <el-upload action="http://localhost:9090/files/upload" :limit="1" :on-success="uploadSuccess" list-type="picture">
            <el-button>上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible=false">关闭</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {reactive,ref} from "vue";
import {Search } from '@element-plus/icons-vue'
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

const handleCurrentChange = ()=>{
  load()
}
const formRef=ref();
const radio2 = ref('1')
const data = reactive({
  name:'',
  username: '',
  age: '',
  sex:'',
  phone:'',
  identi:'',
  avatar:'',
  tableData:[],
  total: 0,
  pageNum: 1, //当前的页码
  pageSize: 10, // 每页个数
  formVisible: false,
  form: {}
})

const uploadSuccess = (res) => {
      ElMessage.success("上传成功");
      data.form.avatar = res.data;
};

const  rules = reactive({
  name:[
    {required: true,message:'请输入姓名',trigger:'blur'},
  ],
  username:[
    {required: true,message:'请输入账号',trigger:'blur'},
  ],
  password:[
    {required: true,message:'请输入密码',trigger:'blur'},
  ],
})
const  load =() =>{
  request.get("/studentIfo/selectPage", {
    params:{
      pageNum: data.pageNum,
      pageSize:data.pageSize,
      name: data.name,
      username: data.username,
      avatar: data.avatar,
    }} ).then(res => {
    data.tableData = res.data?.list || []
    data.total = res.data?.total || 0
  })
}
const reset=() => {
      data.name = ''
      data.username='',
      data.age='',
      data.sex='',
      data.phone='',
      data.identi='',
      data.avatar='',
      load()
}
const  handleAdd=()=>{
  data.form={}
  data.formVisible=true
}
const  save =() =>{
  formRef.value.validate((valid)=>
  {
    if(valid){
      data.form.sex = radio2.value
      request.request({
        url: data.form.id? '/studentIfo/update':'/studentIfo/add',
        method:  data.form.id? 'PUT':'POST',
        data: data.form
      }).then(res=>{
            if(res.code==='200'){
              load()
              data.formVisible=false
              ElMessage.success('操作成功')
            }
            else{
              ElMessage.error(res.msg)
            }
          }
      )
    }
  })}


const editHandler=(row)=>{
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}
const delHandler=(id)=>{
  ElMessageBox.confirm(
      '此操作会永久删除数据，是否确认?',
      '注意！',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        request.delete('/studentIfo/delete/'+ id).then(res=>{
          if(res.code==='200'){
            load()
            ElMessage.success('删除成功')
          } else{
            ElMessage.error(res.msg)
          }
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消删除',
        })
      })
}
load()
</script>
<style>
.listAvatar{
  height: 40px;
  width: 40px;
  border-radius: 8px;
  position: relative;
  right: 5px;
  background-repeat: no-repeat;

}
.cell{
  word-wrap: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 8;
  -webkit-box-orient: vertical;
}
</style>